#include "MeshEntity.h"
#include "SceneNode.h"

namespace egg{

    MeshEntity::MeshEntity(){
        mParamMap.bindNumericParam("uni_mat4_projection");
    }

    void MeshEntity::onAttached(SceneNode* node){
        mParamMap.bindNumericParam(node->getNormalParam());
        mParamMap.bindNumericParam(node->getModelviewParam());
    }

    void MeshEntity::setMaterial(MaterialPtr mat){
        if(mMaterial!=mat){
            mMaterial=mat;
            mParamMap.bindShader(mMaterial->getShader());
        }
    }

    void MeshEntity::setGeometry(GeometryPtr geo){
        if(mGeometry!=geo){
            mGeometry=geo;
        }
    }

    void MeshEntity::_draw(const FrameEvent& evt){
        StateCache::setFillMode(mMaterial->getFillMode());
        mMaterial->getParamMap()->_commitParams(evt.mFrameID);
        mParamMap._commitParams(evt.mFrameID);
        mGeometry->draw();
    }

    RenderPriority MeshEntity::getPriority(){
        if(mMaterial->getOpacity()==1.0f){
            return RP_OPACITY;
        }else{
            return RP_TRANSPARENT;
        }
    }

}